import json
import time

from selenium import webdriver
from appium import webdriver as app_driver
# 定义三个系统的驱动
from selenium.webdriver.common.by import By


class UtilsDriver:
    _media_driver = None
    _back_driver = None
    _app_driver = None
    _quit_back_driver=True

    # 定义自媒体平台的驱动
    @classmethod
    def get_media_driver(cls):
        if cls._media_driver is None:
            cls._media_driver = webdriver.Chrome()
            cls._media_driver.maximize_window()
            cls._media_driver.get("http://ttmp.research.itcast.cn/#/login")
        return cls._media_driver

    # 注销自媒体平台的驱动
    @classmethod
    def quit_media_driver(cls):
        if cls._media_driver is not None:
            cls.get_media_driver().quit()
            cls._media_driver = None

    # 定义后台管理的驱动
    @classmethod
    def get_back_driver(cls):
        if cls._back_driver is None :
            cls._back_driver = webdriver.Chrome()
            cls._back_driver.maximize_window()
            # cls._back_driver.implicitly_wait(10)
            cls._back_driver.get("http://ttmis.research.itcast.cn/")
        return cls._back_driver

    #     注销后台管理的驱动
    @classmethod
    def quit_back_driver(cls):
        if cls._back_driver is not None and cls._quit_back_driver:
            cls.get_back_driver().quit()
            cls._back_driver = None

    #     定义黑马app的驱动
    @classmethod
    def get_app_driver(cls):
        if cls._app_driver is None:
            des_cap = {
                "platformName": "android",
                "deviceName": "****",
                "appPackage": "com.itcast.toutiaoApp",
                "appActivity": ".MainActivity",
                "noReset": True,
                "resetKeyboard": True,
                "unicodeKeyboard": True
            }
            cls._app_driver = app_driver.Remote("http://localhost:4723/wd/hub", des_cap)
        return cls._app_driver

    # 注销黑马app的驱动
    @classmethod
    def quit_app_driver(cls):
        if cls._app_driver is not None:
            cls.get_app_driver().quit()


# 封装一个选择频道的方法
def choose_channel(driver, element, channel):
    element.click()
    time.sleep(1)
    xpath = "//*[@class='el-select-dropdown__wrap el-scrollbar__wrap']//*[text()='{}']".format(channel)
    driver.find_element(By.XPATH, xpath).click()


# 封装一个方法判断元素是否存在
def is_exist(driver, text):
    xpath = "//*[contains(text(), '{}')]".format(text)
    try:
        time.sleep(2)
        return driver.find_element(By.XPATH, xpath)
    except Exception as e:
        return False

# 定义app中边滑动边查找的方法
def app_swipe_find(driver, element, target_ele):
    """
    :param driver: 表示的是app的驱动
    :param element: 表示的滑动的元素对象
    :param target_ele: 表示的是要查找的元素的定位的值
    :return:
    """
    location = element.location  # 获取元素的坐标点位置
    x = location["x"]  # 获取坐标点X的值
    y = location["y"]  # 获取坐标点y的值
    size = element.size
    width = size["width"]
    height = size["height"]
    start_x = x + width*0.9
    end_y = y + height * 0.5
    end_x = x + width * 0.1
    while True:
        page_source = driver.page_source
        try:
            time.sleep(1)
            driver.find_element(*target_ele).click()
            return True
        except Exception as e:
            driver.swipe(start_x, end_y, end_x, end_y, duration=1500)
        if page_source == driver.page_source:
            print("已滑屏到最后的页面，没有找到对应频道！")
            return False

# 获取测试数据
def get_case_data(filename):
    with open(filename,encoding='utf-8') as f:
        case_data=json.load(f)
    list_case_data=[]
    for case in case_data.values():
        data=tuple(case.values())
        list_case_data.append(data)
    return list_case_data


